import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const actions = {}
const mutations = {
    handleUserName: (state, user_name) => {
        state.username = user_name
            // 把登录的用户的名保存到localStorage中，防止页面刷新，导致vuex重新启动，用户名就成为初始值（初始值为空）的情况
        localStorage.setItem('username', user_name)
    },
    handleAvatar: (state, avatar_url) => {
        state.avatar_url = avatar_url
        localStorage.setItem('avatar_url', avatar_url)
    },
    add_api_path: (state, api_path_map) => {
        state.api_path_map = api_path_map
        localStorage.setItem('api_path_map', api_path_map)
    }
}
const state = {
    username: '' || localStorage.getItem('username'),
    avatar_url: localStorage.getItem('avatar_url') || require('./assets/avatar.jpg'),
    api_path_map: {}
}
// getters 只会依赖 state 中的成员去更新
const getters = {
    username: (state) => state.username,
    avatar_url: (state) => state.avatar_url,
    api_path_map: (state) => state.api_path_map
}

const store = new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})
export default store